package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-28 10:18 星期六
 * description:
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsDao goodsDao;

    @Autowired
    SaleListGoodsService saleListGoodsService;

    @Autowired
    CustomerReturnListGoodsService customerReturnListGoodsService;

    /**
     * 根据商品类别  商品编码  商品名称 查询商品的库存   （分页查询）
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> listInventory(Integer page,
                                             Integer rows,
                                             String codeOrName,
                                             Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.getGoodsInventoryList(offSet,rows,codeOrName,goodsTypeId);
        for (Goods goods : goodsList) {
            //销售总量等于销售单据的销售数据减去退货单据的退货数据
            Integer sumSaleGoods = saleListGoodsService.getSumSaleGoods(goods.getGoodsId());
            Integer sumReturnGoods =customerReturnListGoodsService.getSumReturnGoods(goods.getGoodsId());
            goods.setSaleTotal(sumSaleGoods - sumReturnGoods);
        }

        Integer total = goodsDao.getGoodsInventoryCount(codeOrName,goodsTypeId);

        map.put("total",total);
        map.put("rows",goodsList);
        return map;
    }

    /**
     *
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.getGoodsList(offSet,rows,goodsName,goodsTypeId);
        Integer total = goodsDao.getGoodsTotal(goodsName,goodsTypeId);
        map.put("rows",goodsList);
        map.put("total",total);
        return map;
    }

    /**
     * 商品添加或修改
     * @param goods
     * @return
     */
    @Override
    public ServiceVO saveOrUpdate(Goods goods) {

        //设置默认值
        goods.setInventoryQuantity(0);
        goods.setState(0);

        Integer goodsId = goods.getGoodsId();
        if (goodsId != null){
            //修改操作
            goodsDao.update(goods);
        }else{
            //修增操作
            goodsDao.insert(goods);
        }

        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 获取商品编码
     * @return
     */
    @Override
    public ServiceVO getCode() {
        //获取当前商品最大的编码
        String maxCode = goodsDao.getMaxCode();

        Integer goodsCode = Integer.parseInt(maxCode) + 1;

        String unitCode = goodsCode.toString();
        //将编码重新格式化为4为字符串
        for (int i = 4; i > goodsCode.toString().length(); i--) {
            unitCode = "0" + unitCode;
        }

        return new ServiceVO(100,"请求成功",unitCode);
    }

    /**
     * 根据id删除商品 (要判断商品状态,入库、有进货和销售单据的不能删除)
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO delete(Integer goodsId) {

        //要判断商品状态,入库、有进货和销售单据的不能删除
        //根据id商品id获取商品状态
        Integer state = goodsDao.getGoodsState(goodsId);

        if (state != 0){
            return new ServiceVO(402, ErrorCode.HAS_FORM_ERROR_MESS,null);
        }

        goodsDao.delete(goodsId);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 展示无库存商品列表
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.getNoInventoryQuantity(offSet,rows,nameOrCode);
        Integer total = goodsDao.getNoInventoryQuantityCount(nameOrCode);

        map.put("rows",goodsList);
        map.put("total",total);

        return map;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String,Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsDao.getHasInventoryQuantity(offSet,rows,nameOrCode);
        Integer total = goodsDao.getHasInventoryQuantityCount(nameOrCode);

        map.put("rows",goodsList);
        map.put("total",total);
        return map;
    }

    /**
     * 添加库存、修改数量或成本价 (根据goodsId)
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     * @return
     */
    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {

        goodsDao.saveStock(goodsId,inventoryQuantity,purchasingPrice);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {

        //根据id获取state
        Integer goodsState = goodsDao.getGoodsState(goodsId);
        if (goodsState != 0){
            return new ServiceVO(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }

        //删除库存
        goodsDao.deleteStock(goodsId);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }


    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * @return
     */
    @Override
    public Map<String, Object> listAlarm() {
        Map<String,Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.listAlarm();

        map.put("rows",goodsList);
        return map;
    }


    /**
     * 是否修改成功
     * @param goodsCode
     * @param num 增加或减少的数量
     * @return
     */
    @Override
    public boolean changeInventoryQuantity(String goodsCode, Integer num) {
        //获取当前商品的库存数量
        Integer inventoryQuantityNum = goodsDao.getInventoryQuantity(goodsCode);

        //库存不足 修改失败
        if (inventoryQuantityNum + num < 0){
            return false;
        }

        goodsDao.changeInventoryQuantity(goodsCode,num);
        return true;
    }

}
